<template>
  <div class="channel-edit">
    <!-- 我的频道标题 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEditShow = !isEditShow"
        >{{ isEditShow ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <!-- 我的频道内容 -->
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in myChannels"
        :key="index"
        :text="channel.name"
        @click="onMyChannelClick(channel, index)"
      >
        <span class="text" :class="{ active: active === index }" slot="text">{{
          channel.name
        }}</span>
        <!-- 通过插槽自定义图标 -->
        <van-icon
          v-show="isEditShow && !fiexChannel.includes(channel.id)"
          slot="icon"
          name="close"
        ></van-icon>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐标题 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <!-- 频道推荐内容 -->
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        @click="onAddchannel(channel)"
      >
      </van-grid-item>
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from '@/api/article.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道列表
      isEditShow: false, // 控制编辑状态
      fiexChannel: [0] // 固定
    }
  },
  //  // ### 处理展示推荐频道

  // 思路：`所有频道 - 用户频道 = 推荐频道`

  // 1、封装计算属性筛选数据
  computed: {
    recommendChannels () {
      return this.allChannels.filter((channel) => {
        const mychannel = this.myChannels.find((myChannel) => {
          return myChannel.id === channel.id
        })
        return !mychannel
      })
    },
    // 获取user
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    // 加载所有频道
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    // 添加频道
    async onAddchannel (channel) {
      console.log(channel, 123)
      if (this.user) {
        // 已登录
        try {
          const res = await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length
          })
          console.log(res, 777777777777777777777)
          this.$toast('保存成功')
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        // 未登陆
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
      this.myChannels.push(channel)
    },
    //  切换频道功能，删除
    onMyChannelClick (channel, index) {
      // 保存0序号
      if (this.fiexChannel.includes(index)) {
        return
      }
      if (this.isEditShow) {
        // 编辑状态，执行删除
        // console.log(channel, index)
        // 删除前面元素时候，让高亮-1
        if (index <= this.active) {
          this.$emit('update-activ', this.active - 1, true)
        }
        this.myChannels.splice(index, 1)
        // 删除数据持久化
        this.deleteChannel(channel)
      } else {
        //  完成状态，执行跳转操作
        this.$emit('update-activ', index, false)
      }
    },
    // 删除用户频道
    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已经登入
          console.log(channel, 22)
          await deleteUserChannel(channel.id)
        } else {
          // 没有登入
          setItem('TOO-RROI', this.myChannels)
        }
      } catch (err) {
        console.log(err)
        this.$toast('删除失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }
  // 编辑按钮
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-close {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #ccc;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
